package edu.office.jwt;

import edu.office.applicationRepository.ParentRepository;
import edu.office.applicationRepository.StudentRepository;
import edu.office.applicationRepository.TeacherAdminRepository;
import edu.office.entities.Parent;
import edu.office.entities.Student;
import edu.office.entities.TeacherAdmin;
import jakarta.annotation.security.PermitAll;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import org.jasypt.util.password.StrongPasswordEncryptor;
import edu.office.tools.UserWithName;

import java.util.HashMap;
import java.util.List;

/**
 * @author Haogs
 * Dvr4nLcE35V27Xr14h0bQvCBcY4yA3UKjPk8060nGZ9jNXJlrCXege2bZmZsjBjh
 */
@Path("/chklogin")
@ApplicationScoped
public class CheckLogin {
    @Inject
    ParentRepository parentRepository;
    @Inject
    StudentRepository studentRepository;
    @Inject
    TeacherAdminRepository teacherAdminRepository;

    HashMap<Integer, String> roleVirtualIdRoleName = new HashMap<>();
    HashMap<Integer, Integer> roleRealRoles = new HashMap<>();

    @GET
    @Path("/roleinfo")
    @PermitAll
    @Produces(MediaType.APPLICATION_JSON)
    public HashMap<Integer, String> getRoleVirtualIdRoleName() {
        //在页面上显示角色选择时，提供的编号与角色名称，这也为后面到数据库中查找提供数据
        if (roleVirtualIdRoleName.isEmpty()) {
            roleVirtualIdRoleName.put(1, "Student");
            roleVirtualIdRoleName.put(2, "Teacher");
            roleVirtualIdRoleName.put(3, "Secretary");
            roleVirtualIdRoleName.put(4, "Parent");
            roleVirtualIdRoleName.put(5, "Reexamin");
            roleVirtualIdRoleName.put(6, "Admin");
        }

        return roleVirtualIdRoleName;
    }

    public HashMap<Integer, Integer> getRoleRealRoles() {
        if (roleRealRoles.isEmpty()) {
            roleRealRoles.put(1, 3);
            roleRealRoles.put(2, 1);
            roleRealRoles.put(3, 109);
            roleRealRoles.put(4, 112);
            roleRealRoles.put(5, 113);
            roleRealRoles.put(6, 2);
        }
        return roleRealRoles;
    }

    public UserWithName isUserlegal(User4Login user) {
        UserWithName result = new UserWithName();
        boolean legalResult = false;
        if (user.getRoleId() == 1) {//是学生
            List<Student> studentlist = studentRepository.list("NAME", user.getName().trim());
            if (!studentlist.isEmpty()) {
                Student student = studentlist.get(0);
                legalResult = new StrongPasswordEncryptor().checkPassword(student.getPassword(), user.getPassword());//密码也匹配
                if (legalResult) {
                    result.setValid(true);
                    result.setName(user.getName());
                    result.setRoleId(user.getRoleId());
                    result.setFirstName(student.getFirstname());
                    result.setSecondName(student.getSecondname());
                } else {
                    result.setValid(false);
                }
            }
        } else if (user.getRoleId() == 2
                || user.getRoleId() == 3
                || user.getRoleId() == 5
                || user.getRoleId() == 6) {
            List<TeacherAdmin> teacherAdminList = teacherAdminRepository.list("NAME", user.getName());
            if (!teacherAdminList.isEmpty()) {
                TeacherAdmin teacherAdmin = teacherAdminList.get(0);
                legalResult = new StrongPasswordEncryptor().checkPassword(user.getPassword(), teacherAdmin.getPASSWORD());
                if (legalResult && teacherAdmin.getROLE_ID().equals(getRoleRealRoles().get(user.getRoleId()))) {
                    //角色也对
                    result.setValid(true);
                    result.setName(user.getName());
                    result.setRoleId(user.getRoleId());
                    result.setFirstName(teacherAdmin.getFIRSTNAME());
                    result.setSecondName(teacherAdmin.getSECONDNAME());
                } else {
                    result.setValid(false);
                }
            }
        } else if (user.getRoleId() == 4) {
            List<Parent> parentList = parentRepository.list("NAME", user.getName());
            if (!parentList.isEmpty()) {
                Parent parent = parentList.get(0);
                legalResult = new StrongPasswordEncryptor().checkPassword(parent.getPASSWORD(), user.getPassword());//密码也匹配
                if (legalResult) {
                    result.setValid(true);
                    result.setName(user.getName());
                    result.setRoleId(user.getRoleId());
                    result.setFirstName(parent.getFIRSTNAME());
                    result.setSecondName(parent.getSECONDNAME());
                } else {
                    result.setValid(false);
                }
            }
        }
        return result;
    }
}
